package com.maybach.core.ao.impl;

import com.maybach.common.enums.ActLogActorNameEnum;
import com.maybach.common.enums.ActLogBizTypeEnum;
import com.maybach.common.model.ActLogDO;
import com.maybach.core.ao.ActLogAO;
import com.maybach.core.manager.ActLogManager;
import com.maybach.core.security.AdminUser;
import com.maybach.core.security.AdminUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by weishuai on 16/3/11.
 */
@Component
public class ActLogAOImpl implements ActLogAO {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ActLogManager actLogManager;

    @Override
    public void addLog(ActLogBizTypeEnum bizType, Long bizId, String detail, ActLogActorNameEnum title) {
        try {
            AdminUser user = AdminUserUtil.getUser();
            String actorName = user.getRealName();
            Long actorId = user.getAdminId();
            String actorLabel = user.getDepartmentName();

            ActLogDO actLogDO = new ActLogDO();
            actLogDO.setBizType(bizType.name());
            actLogDO.setBizId(bizId);
            actLogDO.setActorName(actorName);
            actLogDO.setActorId(actorId);
            actLogDO.setActorLabel(actorLabel);
            actLogDO.setActDetail(detail);
            actLogDO.setActTitle(title.name());

            actLogManager.insertSelective(actLogDO);
        } catch (Exception e) {
            logger.error("error in addLog,bizType:"+bizType.name()+",bizId"+bizId+",title:"+title, e);
        }
    }
}
